home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / net / spakparnet_0_5.lha / netwin / window.c < prev    next >
C/C++ Source or Header  |  1992-11-09  |  8KB  |  330 lines

  1. /********************************************************************
  2.  ** REALLY SLOPPY TEST SERVER - BUT IT GIVES AN IDEA ON THE EASE
  3.  **
  4.  ** (c) Spak Inc, Darrell Tam, c9107253@ee.newcastle.edu.au (1994)
  5.  ** phone (Australia) 049-829-710
  6.  **                   +49-829-710
  7.  **
  8.  ** (c) SST Scientific Endeavours 1994
  9.  **
  10.  ** LITTLE TEST CLIENT (A "SERVER") FOR RECEIVING & SENDING
  11.  **
  12.  ** BE CAREFUL TO THOROUGHLY TEST YOUR PROGRAMS BECAUSE BADLY FORMED
  13.  ** OR PACKETS BEING USED TWICE AT THE SAME TIME WILL CAUSE THE
  14.  ** SERVER TO PROBABLY DIE - MAKESURE A PACKET IS NOT IN USE BEFORE
  15.  ** SENDING IT (IE WAIT FOR THE REPLY!!!!) AND ALWAYS USE THE SUPPLIED
  16.  ** CALLS (actually #define's) TO INIT THE PACKETS
  17.  **
  18.  **
  19.  ** TABS to 4
  20.  ********************************************************************/
  21.  
  22. #include "/snd/everything.h"
  23. #include "/snd/gfxthings_protos.h"
  24. #include <st/st_proto.h>
  25. #include <st/gadsetup.h>
  26. #include "/par/parstuff.h"
  27. #include "gfxstuff_protos.h"
  28.  
  29.  
  30.  
  31. long out;
  32. struct    IntuitionBase *IntuitionBase = NULL;
  33. struct    GfxBase *GfxBase = NULL;
  34. short    destmachine;
  35. short    running = 1;
  36. short    ourserviceport = SERV_TEST;
  37. struct MsgPort *port = NULL, *theport = NULL, *repport = NULL, *pktport = NULL;
  38.  
  39. short    pressed;
  40.  
  41. short lx, ly;
  42.  
  43.  
  44.  
  45. /** SOME PACKET STUFF */
  46. struct Req {
  47.     /** from here on must be < 118 bytes if it is to fit with the header */
  48. #define REQ_SIZE(a) (sizeof(struct StdReq)+4+1+1+(((a)->n)<<1))
  49.  
  50.     struct    StdReq sr;
  51.     long    type;
  52.     char    lobotomy;
  53.     char    n;                            /* number of shorts following */
  54.     short    xy[20];                        /* number of coords (up to 10)*/
  55. };
  56.  
  57. struct WhatchaPkt {
  58.     struct    PktMsg pm;
  59.     struct    Req sr;
  60. }  wp[2];                                /* double buffered */
  61. short which_wp = 0;
  62. short sent_wp = 0;                        /* if any packet is sent out */
  63. short lastx, lasty;
  64.  
  65.  
  66. /********************************************************************/
  67.     void MaybeSendCurrentPkt(void)
  68. /********************************************************************/
  69. {
  70. struct WhatchaPkt *p = &wp[which_wp];
  71.  
  72. /** SEND OUT THE PACKET IF THE OTHERONE ISN'T OUT, AND THERE IS SOMETHING
  73.  ** IN THIS PACKET */
  74.  
  75.     if(!sent_wp && p->sr.n) {
  76.         sent_wp = 1;
  77.         PREP_PKTMSG_ORIGINAL(&p->pm, destmachine, ourserviceport,
  78.             &p->sr, sizeof(struct Req), /*REQ_SIZE(&p->sr),*/
  79.             NULL, 0,
  80.             NULL, 0,
  81.             NULL, 0);
  82.  
  83.         PutMsg(pktport, p);
  84.  
  85.                 {
  86.                 struct Req *r = &p->sr;
  87.                 short    i;
  88.                     SetAPen(win1->RPort, 1);
  89.                     for(i = 0,
  90.                         Move(win1->RPort, r->xy[i++]-1, r->xy[i++]-1); i < r->n;
  91.                             Draw(win1->RPort,  r->xy[i++]-1, r->xy[i++]-1));
  92.                 }
  93.  
  94.         which_wp ^= 1;                    /* swap packets */
  95.         wp[which_wp].sr.n = 0;            /* and start it off at 0 */
  96.     }
  97. }
  98.  
  99. /********************************************************************/
  100.     void NewCoordSeries(short x, short y)
  101. /********************************************************************/
  102. {
  103. struct Req *r = &wp[which_wp].sr;
  104.     r->n = 0;        /* we may lose coords off the last pkt.... fix later */
  105.     r->xy[r->n++] = x; r->xy[r->n++] = y;
  106.     r->xy[r->n++] = x; r->xy[r->n++] = y;
  107.     lastx = x; lasty = y;
  108.     MaybeSendCurrentPkt();
  109. }
  110.  
  111. /********************************************************************/
  112.     short AddCoord(short x, short y)
  113. /********************************************************************/
  114. {
  115. struct Req *r = &wp[which_wp].sr;
  116. short ret = 1;
  117.     if(r->n == 0) {
  118.         r->xy[r->n++] = lastx; r->xy[r->n++] = lasty;
  119.     }
  120.     if(r->n < 20) {
  121.         r->xy[r->n++] = x; r->xy[r->n++] = y;
  122.         lastx = x; lasty = y;
  123.     }
  124.     else ret = 0;
  125.  
  126.     MaybeSendCurrentPkt();
  127.     return(ret);
  128. }
  129.  
  130.  
  131. /********************************************************************/
  132.                         long HandleWin1(void)
  133. /********************************************************************/
  134. {
  135. struct    IntuiMessage *im;
  136. long    last;
  137.     while (im = (struct IntuiMessage *)GetMsg(win1->UserPort)) {
  138.         switch(last = DoGadgets(&win1gadhold, im)) {
  139.             case 0:
  140.             case -1: break;
  141.  
  142.             case IDCMP_CLOSEWINDOW:
  143.                 LoadRGB4(&(win1->WScreen->ViewPort), savecols, 4);
  144.                 ReplyMsg((struct Message *)im);
  145.                 running = 0;
  146.                 return last;
  147.  
  148.             case IDCMP_NEWSIZE:
  149.                 break;
  150.  
  151.             case IDCMP_ACTIVEWINDOW:
  152.                 WindowToFront(win1);
  153.                 SaveCols(win1, savecols, 32);
  154.                 LoadRGB4(&(win1->WScreen->ViewPort), win1cols, 4);
  155.                 break;
  156.  
  157.             case IDCMP_INACTIVEWINDOW:
  158.                 LoadRGB4(&(win1->WScreen->ViewPort), savecols, 4);
  159.                 break;
  160.  
  161.             case IDCMP_MOUSEBUTTONS:    /* pass back if no gadget was held */
  162.                 if(im->Code & IECODE_LBUTTON) {
  163.                     if(im->Code & IECODE_UP_PREFIX) {
  164.                         pressed = 0;
  165.                         SetAPen(win1->RPort, 2);
  166.                         Move(win1->RPort, lx, ly);
  167.                         Draw(win1->RPort, im->MouseX, im->MouseY);
  168.                         AddCoord(im->MouseX, im->MouseY);
  169.                         ReportMouse(0, win1);
  170.                     }
  171.                     else {
  172.                         pressed = 1;
  173.                         ReportMouse(1, win1);
  174.                         NewCoordSeries(lx = im->MouseX, ly = im->MouseY);
  175.                     }
  176.                 }
  177.                 break;
  178.  
  179.             case IDCMP_MOUSEMOVE:
  180.                 if(pressed) {
  181.                     SetAPen(win1->RPort, 2);
  182.                     Move(win1->RPort, lx, ly);
  183.                     Draw(win1->RPort, im->MouseX, im->MouseY);
  184.                     AddCoord(im->MouseX, im->MouseY);
  185.                     lx = im->MouseX; ly = im->MouseY;
  186.                 }
  187.                 break;
  188.  
  189.             default:
  190.                 break;
  191.         }
  192.         ReplyMsg((struct Message *)im);
  193.     }
  194.     return last;
  195. }
  196.  
  197.  
  198.  
  199.  
  200. /********************************************************************/
  201.     int main(int argc, char *argv[])
  202. /********************************************************************/
  203. {
  204.     out = Output();
  205.  
  206.     destmachine = argv[1][0];
  207.  
  208.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0))
  209.     || !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0)) )
  210.             goto nomore;
  211.  
  212.     if(!OpenGfx()) goto nomore;
  213.  
  214.     if(!(port = CreatePort(0,0)) ||            /* service port */
  215.        !(repport = CreatePort(0,0))) {        /* reply port */
  216.         fpf(out, "No ports\n");
  217.         goto nomore;
  218.     }
  219.  
  220.     if(!(theport = FindPort("ParRegister")) ||
  221.         !(pktport = FindPort("ParServer"))) {
  222.         fpf(out, "Couldn't find par port packet thingy\n");
  223.         goto nomore;
  224.     }
  225.  
  226. /** REGISTER WITH THE PARALLEL THING */
  227. {
  228. struct RegisterMsg rm;
  229.     INIT_REGISTERMSG(&rm, port, RM_ADD_SERVER, ourserviceport, port, NULL); 
  230.     PutMsg(theport, &rm); WaitPort(port); GetMsg(port);
  231.     if(rm.smsg.result != (long)port) {
  232.         fpf(out, "Another service already has this port\n");
  233.         goto nomore;
  234.     }
  235.     fpf(out, "Registered ok as port service (%ld)\n", ourserviceport);
  236. }
  237.  
  238.  
  239.  
  240.     fpf(out, "network Window server: commands sent to '%lc'\n", destmachine);
  241.  
  242.  
  243.     dbzero(wp, sizeof(wp));
  244.     INIT_MSG(&wp[0].pm.smsg.msg, port, sizeof(struct PktMsg));
  245.     INIT_MSG(&wp[1].pm.smsg.msg, port, sizeof(struct PktMsg));
  246.  
  247.  
  248. /** SIT IN A LOOP ON OUR PORT OR DIE ON CTRL C */
  249. {
  250. long mask, waitmask, pmask, rmask, winmask;
  251. struct PktMsgRecv *pmr;
  252.  
  253.     waitmask =    (pmask = 1<<port->mp_SigBit) |
  254.                 (rmask = 1<<repport->mp_SigBit) |
  255.                 (winmask = 1<<win1->UserPort->mp_SigBit) |
  256.                 SIGBREAKF_CTRL_C;
  257.  
  258. /** CAN'T QUIT WITH PACKETS ROAMING AROUND */
  259.     while(sent_wp != 0 || running) {
  260.  
  261.         mask = Wait(waitmask);
  262.  
  263.         if(mask & pmask) {
  264.             while(pmr = (struct PktMsgRecv *)GetMsg(port)) {
  265.  
  266.             /** WE GOT SOMETHING SENT TO US */
  267.                 if(pmr->pm.smsg.type == TYPE_RECEIVED) {
  268.                 struct Req *r = (void *)pmr->req_body;
  269.                 short    i;
  270.                     SetAPen(win1->RPort, 1);
  271.                     for(i = 0,
  272.                         Move(win1->RPort, r->xy[i++], r->xy[i++]); i < r->n;
  273.                             Draw(win1->RPort,  r->xy[i++], r->xy[i++]));
  274.  
  275.                 /*    INIT_MSG(&pmr->pm.smsg.msg,  repport, sizeof(*pmr)); */
  276.                     FREE_PKTMSGRECV(pmr);
  277.                 }
  278.  
  279.             /** WE GOT ONE OF OUR OWN BACK */
  280.                 else if(pmr->pm.smsg.type == TYPE_RETURN) {
  281.                     if(((long)pmr == (long)&wp[0]) || ((long)pmr == (long)&wp[1])) {
  282.                         sent_wp = 0;
  283.                         MaybeSendCurrentPkt();
  284.                     }
  285.                 }
  286.  
  287.             /** SERVER IS SHUTTING DOWN */
  288.                 else if(pmr->pm.smsg.type == TYPE_QUIT) {
  289.                     running = 0;
  290.                     ReplyMsg(pmr);
  291.                 }
  292.             }
  293.         }
  294.  
  295.         if(mask & rmask) {
  296.             while(pmr = (struct PktMsgRecv *)GetMsg(repport)) {
  297.                 fpf(out, "Done: %lx\n", pmr);
  298.             }
  299.         }
  300.  
  301.         if(mask & winmask) HandleWin1();
  302.         if(mask & SIGBREAKF_CTRL_C) running = 0;
  303.     }
  304. }
  305.  
  306.  
  307. endourpain:
  308.  
  309. /** REMOVE US AS A SERVER FROM THE THING */
  310.     fpf(out,"Quitting... (telling server) ");
  311. {
  312. struct RegisterMsg rm;
  313. struct Message *maj;
  314.  
  315.     INIT_REGISTERMSG(&rm, port, RM_REMOVE_SERVER, ourserviceport, NULL, NULL); 
  316.     for(PutMsg(theport, &rm); (WaitPort(port), (maj = GetMsg(port)) != &rm);)
  317.                     ReplyMsg(maj); /* important to return all messages but ours */
  318. }
  319.     fpf(out,"Done\n");
  320.  
  321. nomore:
  322.     CloseGfx();
  323.     if(port) DeletePort(port);
  324.     if(repport) DeletePort(repport);
  325.     if(GfxBase) CloseLibrary((struct Library *)GfxBase);
  326.     if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  327.     return(0);
  328. }
  329.  
  330.